package com.qf.service;

import com.qf.constant.CacheConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * author:z
 * time:2021/12/29 17:24
 * description:限流策略5分钟
 */
@Service(value = "limitFiveMinutes")
@Slf4j
public class LimitChainManagerImpl implements FilterChainService {

    @Autowired
    CacheFeign cacheFeign;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-限流策略]开始执行！");
        //查询限流时间内发送的短信次数统计 limit：clientid：mobilephone 5分钟范围之内
//        Set<Object> limitSize = cacheFeign.zRangeByScore(CacheConstants.CACHE_PREFIX_SMS_LIMIT+standardSubmit.getClientID()+":"+
//                standardSubmit.getDestMobile(),System.currentTimeMillis()-(1000*60*5),System.currentTimeMillis());
//        // 判断5分钟之内的发送次数是否大于规定次数
//        if(limitSize.size()>=3){
//            log.info("[策略模块-限流策略]执行结束！ 发送短信次数限制,稍后再试！");
//            return false;
//        }
//        log.info("[策略模块-限流策略]执行结束！");
        return true;
    }


//    @Override
//    public boolean strategy(StandardSubmit standardSubmit) {
//        log.info("[策略模块-限流策略]开始执行！");
//        //获取客户端信息
//        String key=CacheConstants.CACHE_PREFIX_SMS_LIMIT+standardSubmit.getClientID()+ standardSubmit.getDestMobile();
//        //获取时间
//        long endTime = standardSubmit.getSendTime().getTime();
//        long time = endTime - (1000 * 60 * 5);
//        //
//        Set<Object> result=cacheFeign.zRangeByScore(key,endTime,time);
//        if(result!=null&result.size()>=3){
//            log.info("[策略模块-限流策略]执行结束！ 发送短信次数限制,稍后再试！");
//            return false;
//        }
//
//        log.info("[策略模块-限流策略]执行结束！");
//        return true;
//    }

}


